package com.by.yz.mypersonal;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.Text;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.loginUtil.Constants;
import com.hyphenate.easeui.widget.EaseAlertDialog;

/**
 * Created by 123 on 2016/10/7.
 */
public class PersonalPhoneActivity extends BaseActivity {

    private Button phone;
    private TextView Customer;
    final int REQUEST_CODE_ASK_CALL_PHONE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_activity_phone);
        setToolbarTitle("正和医生客服");

        initView();
    }

    private void initView() {
        Customer = (TextView) findViewById(R.id.customer);
        Customer.setText(Constants.CUSTOMER);
        phone = (Button) findViewById(R.id.btn_call);
        phone.setOnClickListener(new View.OnClickListener() {
            @Override   public void onClick(View v) {
                requestPermission();
            }
        });
    }

    /**
     * 申请权限
     */
    private void requestPermission(){
        //判断Android版本是否大于23
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},
                        REQUEST_CODE_ASK_CALL_PHONE);
                return;
            }else{
                callPhone();
            }
        }else{
            callPhone();
        }
    }

    /**
     * 注册权限申请回调
     * @param requestCode 申请码
     * @param permissions 申请的权限
     * @param grantResults 结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
        switch (requestCode){
            case  REQUEST_CODE_ASK_CALL_PHONE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    callPhone();
                }else {
                    new EaseAlertDialog(this, "电话权限被禁止，请开启权限后重试").show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void callPhone(){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+Constants.CUSTOMER));
        startActivity(intent);
    }

}
